package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.example.demo.base.EntityWrapperDecorator;
import com.example.demo.base.Page;
import com.example.demo.base.WebResult;
import com.example.demo.dto.UserDTO;
import com.example.demo.dto.UserUpdateDTO;
import com.example.demo.entity.UserEntity;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.example.demo.util.BeanCopyUtil;
import com.example.demo.vo.UserAddVO;
import com.example.demo.vo.UserPageVO;
import com.example.demo.vo.UserUpdateVO;
import com.example.demo.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 用户service
 */
@Service("userServiceImpl")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public WebResult updateUser(UserUpdateVO userUpdateVO) {
        //校验用户是否存在
        UserEntity entity = userMapper.selectById(userUpdateVO.getId());
        if (entity == null) {
            return WebResult.fail("401", "ID为[" + userUpdateVO.getId() + "]的用户不存在");
        }

        //更新信息
        UserEntity userEntity = BeanCopyUtil.copyToClass(userUpdateVO, UserEntity.class);
        userEntity.setId(userUpdateVO.getId());
        userMapper.updateById(userEntity);

        //返回信息
        UserUpdateDTO userUpdateDTO = BeanCopyUtil.copyToClass(userEntity, UserUpdateDTO.class);
        userUpdateDTO.setId(userEntity.getId());
        return WebResult.ok(userUpdateDTO);
    }

    @Override
    public WebResult listUser(UserVO userVO) {
        EntityWrapperDecorator<UserEntity> wrapperDecorator = new EntityWrapperDecorator<>();
        wrapperDecorator.eqNotNull(UserEntity.Column.USER_NAME.name(), userVO.getUserName());
        wrapperDecorator.eqNotNull(UserEntity.Column.ID.name(), userVO.getId());
        wrapperDecorator.eqNotNull(UserEntity.Column.NICK_NAME.name(), userVO.getNickName());
        List<UserEntity> userEntityList = userMapper.selectList(wrapperDecorator);
        return WebResult.ok(BeanCopyUtil.copyList(userEntityList, UserDTO.class));
    }

    @Override
    public WebResult pageUser(UserPageVO userPageVO) {
        EntityWrapperDecorator<UserEntity> wrapperDecorator = new EntityWrapperDecorator<>();
        wrapperDecorator.eqNotNull(UserEntity.Column.ID.name(), userPageVO.getId());
        wrapperDecorator.likeNotNull(UserEntity.Column.MOBILE_PHONE.name(), userPageVO.getMobilePhone());
        wrapperDecorator.likeNotNull(UserEntity.Column.NICK_NAME.name(), userPageVO.getNickName());
        wrapperDecorator.likeNotNull(UserEntity.Column.USER_NAME.name(), userPageVO.getUserName());
        wrapperDecorator.orderByDesc(UserEntity.Column.CREATE_DATE.name());
        Pagination pagination = userPageVO.toPagination();
        List<UserEntity> userEntityList = userMapper.selectPage(pagination, wrapperDecorator);
        List<UserDTO> userDTOList = BeanCopyUtil.copyList(userEntityList, UserDTO.class);
        return WebResult.ok(new Page<>(userDTOList, pagination));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WebResult addUser(UserAddVO userAddVO) {
        boolean existsUserName = this.existsUserName(userAddVO.getUserName());
        if (existsUserName) {
            return WebResult.fail("401", "用户名[" + userAddVO.getUserName() + "]已存在");
        }
        UserEntity userEntity = new UserEntity();
        BeanUtil.copyProperties(userAddVO, userEntity);
        userMapper.insert(userEntity);
        return WebResult.ok(userEntity.getId());
    }

    private boolean existsUserName(String userName) {
        Assert.hasText(userName, "用户名不能为空");
        EntityWrapper<UserEntity> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq(UserEntity.Column.USER_NAME.name(), userName);
        return userMapper.selectCount(entityWrapper) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WebResult deleteUserById(Long id) {
        return WebResult.ok(userMapper.deleteById(id));
    }
}
